package com.dykj.fireFighting.web.buildmanger;

import com.dykj.fireFighting.api.socialUnit.buildmanger.dto.FloorDto;
import com.dykj.fireFighting.api.socialUnit.buildmanger.entity.Floor;
import com.dykj.fireFighting.api.socialUnit.buildmanger.service.IFloorService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 建筑管理-楼层接口
 *
 * @Author aixiaolan
 * @Date 2021-06-23 09:44:33
 */
@Slf4j
@Api(value = "建筑管理-楼层接口", tags = {"建筑管理-楼层接口"})
@RestController
@RequestMapping("buildmanger/floor")
public class FloorController {

    @DubboReference
    private IFloorService floorService;

    /**
     * 获取单位id
     *
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     *
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 建筑管理-楼层列表
     *
     * @return
     */
    @AutoLog(value = "建筑管理-楼层-分页列表查询")
    @ApiOperation(value = "建筑管理-楼层分页列表", notes = "建筑管理-楼层分页列表")
    @GetMapping("/list")
//    @RequiresPermissions("buildmanger/floor/list")
    public CommonResult<PageVo<Floor>> treeGrid(FloorDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<Floor> page = floorService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 建筑管理-楼栋管理列表下拉选择
     *
     * @return
     */
    @AutoLog(value = "建筑管理-楼层管理-分页列表查询")
    @ApiOperation(value = "建筑管理-楼栋=层管理分页列表", notes = "建筑管理-楼层管理分页列表")
    @GetMapping("/list2")
    public CommonResult<List<Floor>> getFloorList() {
        List<Floor> page = floorService.getFloorList(getEnterpriseId());
        log.info("楼层查询条数结果：【{}】", page.size());
        return CommonResult.success(page);
    }


    /**
     * 获取建筑管理-楼层信息
     */
    @AutoLog(value = "建筑管理-楼层-获取建筑管理-楼层信息")
    @ApiOperation(value = "获取建筑管理-楼层信息", notes = "获取建筑管理-楼层信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
//    @RequiresPermissions("buildmanger/floor/info")
    public CommonResult<Floor> info(@PathVariable("id") String id) {
        Floor floor = floorService.getById(id);
        return CommonResult.success(floor);
    }

    /**
     * 保存建筑管理-楼层信息
     */
    @AutoLog(value = "建筑管理-楼层-保存建筑管理-楼层信息")
    @ApiOperation(value = "保存建筑管理-楼层信息", notes = "保存建筑管理-楼层信息")
    @PostMapping("/save")
//    @RequiresPermissions("buildmanger/floor/save")
    public CommonResult save(@Valid @RequestBody Floor floor) {
        try {
            floor.setCreateTime(new Date());
            floor.setUpdateTime(new Date());
            floor.setCreateBy(getName());
            floor.setUnitId(getEnterpriseId());
            floorService.save(floor);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改建筑管理-楼层信息
     */
    @AutoLog(value = "建筑管理-楼层-修改建筑管理-楼层信息")
    @ApiOperation(value = "修改建筑管理-楼层信息", notes = "修改建筑管理-楼层信息")
    @PostMapping("/update")
//    @RequiresPermissions("buildmanger/floor/update")
    public CommonResult update(@Valid @RequestBody Floor floor) {
        try {
            floor.setUpdateTime(new Date());
            floor.setUpdateBy(getName());
            floorService.updateById(floor);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除建筑管理-楼层信息
     */
    @AutoLog(value = "建筑管理-楼层-删除建筑管理-楼层信息")
    @ApiOperation(value = "删除建筑管理-楼层信息", notes = "删除建筑管理-楼层信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
//    @RequiresPermissions("buildmanger/floor/delete")
    public CommonResult delete(@RequestBody String[] ids) {
        try {
            floorService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
